package com.mall507.fivepeaks.widget.xwebview;

import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.hzh.frame.ui.activity.BaseUI;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.activity.MainUI;
import com.mall507.fivepeaks.util.Util;

public class XWebViewToMainActivity extends BaseUI {
	private XWebView webview;
	private ProgressBar pb;
	private RelativeLayout rc;
	private String titleName = "", url = "http://oneblack.net";
	private ValueCallback<Uri> mUploadMessage;
	private final static int FILECHOOSER_RESULTCODE = 1;

	@Override
	protected void onCreateBase() {
		if (getIntent().getExtras() != null) {
			if(null!=getIntent().getStringExtra("title")){
				titleName = getIntent().getStringExtra("title");
			}
			if(null!=getIntent().getStringExtra("url")){
				url = getIntent().getStringExtra("url");
			}
		}
		setContentView(R.layout.view_xwebview);
		getTitleView().setContent(titleName);
		getTitleView().setLeftOnclickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(XWebViewToMainActivity.this,MainUI.class);
				startActivity(intent);
				finish();
			}
		});
		rc = (RelativeLayout) findViewById(R.id.public_pleasewait);
		pb = (ProgressBar) findViewById(R.id.progressBar1);
		webview = (XWebView) findViewById(R.id.lisence1);
		// 设置WebView属性，每次及时加载 不要缓存
		webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		// 设置WebView属性，能够执行Javascript脚本
		webview.getSettings().setJavaScriptEnabled(true);
        //设置可以访问文件  
		webview.getSettings().setAllowFileAccess(true);  
		// 加载需要显示的网页
		try {
			webview.setWebViewClient(new WebViewClient() {
				@Override
				public void onPageFinished(WebView view, String url) {
					super.onPageFinished(view, url);
					// 网页加载完毕
					pb.setVisibility(View.GONE);
					rc.setVisibility(View.GONE);
				}
		        @Override
		        public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
		        	super.onReceivedError(view, errorCode, description, failingUrl);
		        	webview.loadUrl("file:///android_asset/errorpage/error.html");
		        }
			});
			webview.setWebChromeClient(new WebChromeClient(){
				@Override
				public void onReceivedTitle(WebView view, String title) {
					super.onReceivedTitle(view, title);
					//获取网页标题
					if(!Util.isEmpty(title) && "".equals(titleName)){
						getTitleView().setContent(title);
					}
				}
			});
			webview.loadUrl(url);
		} catch (Exception e) {
		}
	}
	
	protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
		if (requestCode == FILECHOOSER_RESULTCODE) {
			if (null == mUploadMessage) return;
			Uri result = intent == null || resultCode != RESULT_OK ? null: intent.getData();
			mUploadMessage.onReceiveValue(result);
			mUploadMessage = null;
		}
	}

	// 刷新
	public void reload(View view) {
		webview.reload();
	}
	// 后退
	public void goBack(View view) {
		webview.goBack();
	}
	// 前进
	public void goForward(View view) {
		webview.goForward();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			Intent intent = new Intent(XWebViewToMainActivity.this,MainUI.class);
			startActivity(intent);
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
